package com.java.controller;
import com.github.pagehelper.PageInfo;
import com.java.model.*;
import com.java.service.LaunchService;
import com.java.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LaunchService launchService;

    @RequestMapping("/openLogin")
    public String openLogin() {
        return "user/login";
    }
//自媒体登录
    @RequestMapping("/login")
    @ResponseBody
    public Result login(HttpServletRequest request,String phone2, String password2, Model model){
        Media media = new Media();
        media.setPhoneNum(phone2);
        media.setPassword(password2);
        return userService.login(media,request,model);
    }
    //后台投放页面
    @RequestMapping("/openList")
    public String resourceList(HttpServletRequest request, Model model){
        Media media =(Media) request.getSession().getAttribute("user");
        List<Status> state = launchService.state();
        model.addAttribute("state",state);
        model.addAttribute("id",media.getId());
        return "/user/launch/list";
    }
    //后台投放页面
    @RequestMapping("/forwardList")
    public String forward(HttpServletRequest request, Model model){
        Media media =(Media) request.getSession().getAttribute("user");
        List<Status> state = launchService.state();
        model.addAttribute("state",state);
        model.addAttribute("id",media.getId());
        return "/user/launch/forward";
    }
    /*媒体组管理投放的申请单*/
    @RequestMapping("/launch/list")
    @ResponseBody
    public Result list(Apply apply){
        PageInfo<Apply> pageInfo = launchService.applyList(apply);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }
    /*媒体组管理投放的预约单*/
    @RequestMapping("/launch/forward")
    @ResponseBody
    public Result list3(Apply apply){
        PageInfo<Apply> pageInfo = launchService.forwardList(apply);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }
    /*管理员管理投放的申请单*/
    @RequestMapping("/list")
    @ResponseBody
    public Result list2(Apply apply){
        PageInfo<Apply> pageInfo = launchService.adminList(apply);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }

    /*管理员同意申请单*/
    @RequestMapping("/yes2")
    @ResponseBody
    public Result yes2(Apply apply) {
        apply.setStatus(4);
        return launchService.yes2(apply);
    }

    /*管理员拒绝申请单*/
    @RequestMapping("/no2")
    @ResponseBody
    public Result no2(Apply apply) {
        apply.setStatus(6);
        return launchService.yes(apply);
    }

    /*媒体组同意申请单*/
    @RequestMapping("/yes")
    @ResponseBody
    public Result yes(int id) {
        Apply apply = new Apply();
        apply.setId(id);
        apply.setStatus(2);
        return launchService.yes(apply);
    }

    @RequestMapping("/success")
    @ResponseBody
    public Result success(int id) {
        Apply apply = new Apply();
        apply.setId(id);
        apply.setStatus(3);
        return launchService.yes(apply);
    }

    /*媒体组拒绝申请单*/
    @RequestMapping("/no")
    @ResponseBody
    public Result no(Apply apply) {
        apply.setStatus(5);
        return launchService.yes(apply);
    }


    @RequestMapping("/index")
    public String index() {
        return "user/launch/index";
    }

}
